/**
 * BaseAction.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util.base.action;

import java.net.URLEncoder;
import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.site.model.master.FrontUser;
import com.site.util.PropertyUtil;
import com.site.util.RSAUtil;
import com.site.util.StringUtils;
import com.site.util.constant.Constants;


/**
 * 该类做为所有Action的基类,所有的action都应该继承此类。
 * 便于异常处理，文件上传，分页显示等多种功能的实现。
 * 
 * 在使用服务器端验证器时，如果某些方法不需要验证，则使用注解：@SkipValidation

 */
public abstract class FrontendBaseAction extends BaseAction {	
	
	private static final long serialVersionUID = -6328623754831503735L;
	
	/**
	 * 记住URL
	 */
	protected String rememberURL;
	
	/**
	 * 操作后输出的消息
	 */
	private String opertionMessage;
	
	private FrontUser curUser;
	
	private String module;

	private String expoent;
	
	// 确定 url
	private String confirmUrl;
	// 返回 url
	private String returnUrl;
	// 选择 title
	private String selectTitle;
	
	
	/**
	 * 获取当前登录用户信息(前台)
	 * @return
	 */
	public FrontUser getCurrentFrontUser() {
		curUser = (FrontUser)request.getSession().getAttribute(Constants.LOGIN_USER_FRONTEND);
		return curUser;
	}
	
	/**
	 * （加密之前初始化）准备密码对
	 * @return
	 */
	public void initEncryptData() {
		RSAPublicKey rsap;
		try {
			//密码对
			KeyPair keyPair = (KeyPair)sessionMap.get(Constants.SITE_RSA_KEY_FILE);
			if(keyPair == null) {
				String path = new String();
				path = request.getSession().getServletContext().getRealPath("/")+PropertyUtil.getPropertyValueByKey(Constants.SITE_RSA_KEY_PATH,request);
				keyPair = (KeyPair) RSAUtil.getKeyPair(path);
				sessionMap.put(Constants.SITE_RSA_KEY_FILE, keyPair);
				if(path!=null){
					path = null;
				}
			}
			
			//准备公钥，用户需要公钥加密
			rsap = (RSAPublicKey)keyPair.getPublic();
			module = rsap.getModulus().toString(16);
			expoent = rsap.getPublicExponent().toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解密方法
	 * @param encryptPwd
	 * @return
	 */
	public String getDecryptPwd(String encryptPwd){
		try{
		  	KeyPair keyPair = (KeyPair)sessionMap.get(Constants.SITE_RSA_KEY_FILE);
			if(keyPair == null) {
				String path = new String();
				path = request.getSession().getServletContext().getRealPath("/")+PropertyUtil.getPropertyValueByKey(Constants.SITE_RSA_KEY_PATH,request);
				keyPair = (KeyPair) RSAUtil.getKeyPair(path);
				sessionMap.put(Constants.SITE_RSA_KEY_FILE, keyPair);
				if(path!=null){
					path = null;
				}
			}
			//私钥解密
			return RSAUtil.decryptPwdFromJS(keyPair.getPrivate(), encryptPwd);
		} catch (Exception e) {
			log.info("解密出错 密码：" + encryptPwd, e);
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 前台未登录时组装需要跳转的URL路径
	 * pf 20120308
	 */
	public void remURL2() {
		String url=request.getRequestURI();
		String contextPath=request.getContextPath();
		url=url.substring(contextPath.length());
		Map<String, String[]> paraMap=request.getParameterMap();
		if(paraMap.size()>0)
		{
			url+="?";
			for(Object o : paraMap.keySet())
			{
				/*Object value=(Object)paraMap.get(o);
				if(o.toString().startsWith("method:"))
					continue;
				if (!"".equals(request.getParameter(o.toString()))) {
					url+=o.toString()+"="+URLEncoder.encode(request.getParameter(o.toString())+"&");
				}*/
				
				String[] valueStr = paraMap.get(o);
				String value = StringUtils.join(",", valueStr);
				if(o.toString().startsWith("method:"))
					continue;
				if (!"".equals(value)) {
					url+=o.toString()+"="+URLEncoder.encode(value+"&");
				}
			}
		}
		if (url.endsWith(URLEncoder.encode("&"))) {
			url = url.substring(0, url.lastIndexOf(URLEncoder.encode("&")));
		}
		request.setAttribute("req_url", url);
	}
	public void remURL(HttpServletRequest request)
	{
		//User curUser = getCurrentUser();
		//if (curUser == null) {
			String url=request.getRequestURI();
			String contextPath=request.getContextPath();
			url=url.substring(contextPath.length());
			Map<String, String[]> paraMap=request.getParameterMap();
			if(paraMap.size()>0)
			{
				url+="?";
				for(Object o : paraMap.keySet())
				{
					if (!"".equals(request.getParameter(o.toString()))) {
						url+=o.toString()+"="+URLEncoder.encode(request.getParameter(o.toString())+"&");
					}
				}
			}
			if (url.endsWith(URLEncoder.encode("&"))) {
				url = url.substring(0, url.lastIndexOf(URLEncoder.encode("&")));
			}
			request.setAttribute("req_url", url);
			//if(!request.isRequestedSessionIdValid())
			//	addActionError("超时，请重新登录");
		//}
	}
	public void setRememberURL(String rememberURL) {
		this.rememberURL = rememberURL;//.replace("__", "&");
	}

	public String getRememberURL() {
		return rememberURL;
	}
	 
	public void setOpertionMessage(String opertionMessage) {
		this.opertionMessage = opertionMessage;
	}

	public String getOpertionMessage() {
		return opertionMessage;
	}

	public FrontUser getCurUser() {
		return curUser;
	}

	public void setCurUser(FrontUser curUser) {
		this.curUser = curUser;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getExpoent() {
		return expoent;
	}

	public void setExpoent(String expoent) {
		this.expoent = expoent;
	}

	public String getConfirmUrl() {
		return confirmUrl;
	}

	public void setConfirmUrl(String confirmUrl) {
		this.confirmUrl = confirmUrl;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public String getSelectTitle() {
		return selectTitle;
	}

	public void setSelectTitle(String selectTitle) {
		this.selectTitle = selectTitle;
	}
}
